package com.cheyun.netsalev3.utils

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.BR
import com.cheyun.netsalev3.viewmodel.BaseViewModel

/**
 * @author Michael
 * @date  2018/12/10 - 16:42.
 */
abstract class BaseFragment<T : ViewDataBinding> : Fragment(){
    lateinit var binding: T
    private lateinit var viewModel: ViewModel
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        Log.d(TAG, "onCreateView: ")
        binding = DataBindingUtil.inflate(inflater, initContentView(), container, false)
        viewModel = if (initViewModel() == null) {
            ViewModelProviders.of(this).get(BaseViewModel::class.java)
        } else {
            initViewModel()!!
        }
        binding.setVariable(initVariableId(), viewModel)
        binding.setLifecycleOwner(this)
        initToast()
        initView()
        initData()
        return binding.root
    }

    /**
     * binding是否初始化
     */
     fun isBindingInit(): Boolean {
        return ::binding.isInitialized
    }


    /**
     * 公用Toast 用于消息提示
     */
    private fun initToast() {
        if (this.isVisible) {
            if (BaseViewModel::class.java.isInstance(viewModel)) {
                var baseViewModel = viewModel as BaseViewModel
                baseViewModel.toast.observe(this, Observer {
                    Log.e("FragmentToast", "$it:$this")
                    Toast.makeText(context, it, Toast.LENGTH_LONG).show()
                })
            }
        }
    }

    /**
     * 返回布局id
     */
    abstract fun initContentView(): Int

    /**
     * 返回ViewMode
     */
    open fun initViewModel(): ViewModel? {
        return null
    }

    /**
     * 返回绑定的VariableId 默认viewMode 可以覆盖重写
     */
    open fun initVariableId(): Int {
        return BR.viewModel
    }

    /**
     * 初始化view
     */
    abstract fun initView()

    /**
     * 初始化data
     */
    abstract fun initData()


    protected var TAG = javaClass.simpleName

    private var isLoaded = false

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume: ")
        if (!isLoaded && !isHidden) {
            lazyInit()
            Log.d(TAG, "lazyInit:!!!!!!!")
            isLoaded = true
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Log.d(TAG, "onDestroyView: ")
        isLoaded = false
    }

    abstract fun lazyInit()

    override fun onAttach(context: Context) {
        super.onAttach(context)
        Log.d(TAG, "onAttach: ")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "onCreate: ")
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.d(TAG, "onViewCreated: ")
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        Log.d(TAG, "onActivityCreated: ")
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart: ")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause: ")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop: ")
    }

    override fun onDetach() {
        super.onDetach()
        Log.d(TAG, "onDetach: ")
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        Log.d(TAG, "onHiddenChanged:hidden-->$hidden")
    }
}